package com.yanghui.service.impl;

import com.yanghui.dao.ResourceMapper;
import com.yanghui.pojo.Resource;
import com.yanghui.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ResourceServiceImpl implements ResourceService {
    @Autowired
    private ResourceMapper resourceMapper;


    /**
     * 添加资源记录
     */
    @Override
    public int addResource(Resource resource) {
        return resourceMapper.insertSelective(resource);
    }

    /**
     * 按照ID删除某条资源信息
     */
    @Override
    public int deleteResourceById(Integer id) {
        return resourceMapper.deleteByPrimaryKey(id);
    }

    /**
     * 按照Id修改资源信息
     */
    @Override
    public int updateResourceById(Resource resource) {
        /** 如果是空串，则不更新 */
        if ("".equals(resource.getTitle()) == true) {
            resource.setTitle(null);
        }
        if ("".equals(resource.getLink()) == true) {
            resource.setLink(null);
        }
        if ("".equals(resource.getContent()) == true) {
            System.out.println(1);
            resource.setContent(null);
        }
        if ("".equals(resource.getPassword()) == true) {
            resource.setPassword(null);
        }

        return resourceMapper.updateByPrimaryKeySelective(resource);
    }

    /**
     * 按照id查询资源记录
     */
    @Override
    public Resource selectResourceById(Integer id) {
        return resourceMapper.selectByPrimaryKey(id);
    }

    /**
     * 按照查询条件查询资源记录
     */
    @Override
    public List<Resource> selectLikeResourceListByPage(Map<String, Object> map) {
        return resourceMapper.selectLikeResourceListByPage(map);
    }

    /**
     * 按审核状态分组查询数量
     */
    @Override
    public List<?> selectResourceListByStatus() {
        return resourceMapper.selectResourceListByStatus();
    }
}
